package com.cskaoyan.proxy;

import com.cskaoyan.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 按照ProxyFactoryBean的风格自定义的通用的代理生成工厂
 * @author stone
 * @date 2021/11/19 14:51
 */

@Data//👉 set方法
public class ServiceProxyFactoryBean2 implements FactoryBean {

    Object target;//委托类组件 👉 target

    Class type;

    @Override
    public Object getObject() throws Exception {
        Object proxy = Enhancer.create(target.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                System.out.println("正道的光");
                Object invoke = method.invoke(target, objects);
                System.out.println("照在大地上");
                return invoke;
            }
        });
        return proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return type;
    }
}
